package com.iscreen.dbupgrade;

import com.iscreen.dbupgrade.constants.schema.TableSchemaAudit;
import com.iscreen.utils.db.connection.DBConnection;
import com.iscreen.utils.exceptions.IScreenException;

public class SchemaInitiliazer
{
	private final DBConnection connection = DBUpgrade.getInstance()
			.getConnection();
	
//	private Logger logger = Logger.getLogger(SchemaInitiliazer.class);

//	private void createSchemaAuditTable() throws IScreenException
//	{
//		CreateTableQuery createSchemaAuditTable = DBUpgrade.getInstance()
//				.getFactory().getQueryFactory().createCreateTableQuery(
//						TableSchemaAudit.NAME, "Schema operations audit");
//		createSchemaAuditTable.addField(new NewField(
//				TableSchemaAudit.FLD_STEP_ID, FieldType.ID, true,
//				"Upgrade step id"));
//		createSchemaAuditTable.addField(new NewField(
//				TableSchemaAudit.FLD_COMMAND_ID, FieldType.ID, true,
//				"Upgrade command id"));
//		createSchemaAuditTable.addField(new NewField(
//				TableSchemaAudit.FLD_OPERATION, FieldType.STRING, 200, true,
//				"Upgrade operation description"));
//		createSchemaAuditTable.addField(new NewField(TableSchemaAudit.FLD_SQL,
//				FieldType.STRING, 2000, false, "Upgrade operation SQL"));
//		createSchemaAuditTable.addField(new NewField(
//				TableSchemaAudit.FLD_STATUS, FieldType.STRING, 20, true,
//				"Upgrade operation status"));
//
//		String[] pkFields = { TableSchemaAudit.FLD_STEP_ID,
//				TableSchemaAudit.FLD_COMMAND_ID };
//		createSchemaAuditTable.createPrimaryKey(Arrays.asList(pkFields));
//
//		Query query = connection.createQuery(createSchemaAuditTable.getSql());
//		try
//		{
//			query.execute();
//		}
//		finally
//		{
//			query.close();
//		}
//		logger.info("Table " + TableSchemaAudit.NAME + " created.");
//	}

	private void verifySchemaAuditTableExists() throws IScreenException
	{
		if (!SchemaUtils.isTableExists(connection, TableSchemaAudit.NAME))
		{
//			createSchemaAuditTable();
		}

	}

	public void init() throws IScreenException
	{
		verifySchemaAuditTableExists();
	}

}
